package com.hok.misc.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;


public class FTPTest {
	public static void main(String[] args)  {
//		 testUpload();
//		testDownload();
//		deleteFile();
		try {
			//FTPUtil.uploadFile("10.10.12.11", "billing", "billing", "D:/workspace/mytest/bin/com/ftp/CommandExecutor.class");
			FTPUtil.listFile("10.10.12.11", "billing", "billing", null);
		} catch (FTPException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}

	/**
	 * FTP�ϴ������ļ�����
	 */
	public static void testUpload() {
		FTPClient ftpClient = new FTPClient();
		FileInputStream fis = null;

		try {
			ftpClient.connect("10.10.12.11");
			ftpClient.login("billing", "billing");

			File srcFile = new File("D://temp/t2.txt");
			fis = new FileInputStream(srcFile);
			// �����ϴ�Ŀ¼
			ftpClient.changeWorkingDirectory("/home/bill/billing/temp");
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("UTF-8");
			// �����ļ����ͣ������ƣ�
			// ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
			ftpClient.cwd("temp");
			ftpClient.storeFile(srcFile.getName(), fis);
			System.out.println(srcFile.getName());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP�ͻ��˳��?", e);
		} finally {
			IOUtils.closeQuietly(fis);
			try {
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException("�ر�FTP���ӷ����쳣��", e);
			}
		}
	}

	/**
	 * FTP���ص����ļ�����
	 */
	public static void testDownload() {
		FTPClient ftpClient = new FTPClient();
		FileOutputStream fos = null;

		try {
			ftpClient.connect("10.10.12.11");
			ftpClient.login("billing", "billing");

			String remoteFileName = "/temp/t2.txt";///home/bill/billing
			fos = new FileOutputStream("c:/t2.txt");

			ftpClient.setBufferSize(1024);
			// �����ļ����ͣ������ƣ�
			ftpClient.setControlEncoding("UTF-8");
			ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
			System.out.println(ftpClient.retrieveFile(remoteFileName, fos));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP�ͻ��˳��?", e);
		} finally {
			IOUtils.closeQuietly(fos);
			try {
				ftpClient.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException("�ر�FTP���ӷ����쳣��", e);
			}
		}
	}

	/**
	 * ɾ��һ���ļ�
	 */
	public static boolean deleteFile() {
		boolean flag = true;
		try {
			String remoteFileName = "/home/bill/billing/temp/t1.txt";
			FTPClient ftpClient = new FTPClient();
			ftpClient.connect("10.10.12.11");
			ftpClient.login("billing", "billing");
//			int i = ftpClient.sendCommand("rm", "/home/bill/billing/temp/t1.txt");
//			System.out.println(i);
			flag = ftpClient.deleteFile(remoteFileName);
			if (flag) {
				System.out.println("ɾ���ļ��ɹ���");
			} else {
				System.out.println("ɾ���ļ�ʧ�ܣ�");
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return flag;
	}

}
